#include "widget.h"
#include "ui_widget.h"
#include <QMouseEvent>
#include <QPainter>
#include <QMovie>
#include <QStandardPaths>
#include <QFileDialog>
#include <QUrl>

Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
  ui->setupUi(this);
  setMouseTracking(true);
  move(400,400);
  resize(250,344);
  //resize(320,338);

  //设置透明
  setWindowFlags(Qt::FramelessWindowHint//|Qt::Tool|Qt::SubWindow
                 |windowFlags());
  setAttribute(Qt::WA_TranslucentBackground);

  //设置无边框
  Qt::WindowFlags m_flags = windowFlags();
  setWindowFlags(m_flags | Qt::WindowStaysOnTopHint);
  show();


  p2.resize(100,90);

  Qt::WindowFlags m_flags2 = windowFlags();
  p2.setWindowFlags(m_flags2 | Qt::WindowStaysOnTopHint);

  p2_p1.setParent(&p2);
  p2_p1.resize(100,30);
  p2_p1.move(0,0);
  p2_p1.setText(QObject::tr("点错了"));

  p2_p2.setParent(&p2);
  p2_p2.resize(100,30);
  p2_p2.move(0,30);
  p2_p2.setText(QObject::tr("唱歌"));

  p2_p3.setParent(&p2);
  p2_p3.resize(100,30);
  p2_p3.move(0,60);
  p2_p3.setText(QObject::tr("关闭"));


  tap.setPlaybackMode(QMediaPlaylist::Random);
  player.setPlaylist(&tap);

  connect(&p2_p1,&QPushButton::released,[=]{
    p2.close();
  });
  connect(&p2_p2,&QPushButton::released,this,&Widget::bo);
  connect(&p2_p3,&QPushButton::released,[=]{
    close();
    p2.close();
  });
}

Widget::~Widget()
{
  delete ui;
}

void Widget::mousePressEvent(QMouseEvent *ev)
{
  mouse_flag=1;
  mouse_x=ev->x();
  mouse_y=ev->y();
  qDebug("x=%d",mouse_x);
  qDebug("y=%d",mouse_y);
  if(ev->buttons()==Qt::RightButton)
  {
    p2.move(mouse_x+x(),mouse_y+y());
    p2.show();
  }
}

void Widget::mouseReleaseEvent(QMouseEvent *ev)
{
  mouse_flag=0;
}

void Widget::mouseMoveEvent(QMouseEvent *ev)
{

  if(mouse_flag==1)
  {
    int x=this->x()+ev->x();
    int y=this->y()+ev->y();
    move(x-mouse_x,y-mouse_y);
  }
}

void Widget::paintEvent(QPaintEvent *e)
{
  QPainter paint(this);
  paint.drawPixmap(rect(),QPixmap("image/3.png"));



}

void Widget::bo()
{
  p2.close();
  /*QString Name=QDir::homePath();
  QString pathList=QFileDialog::getOpenFileName(this,"选择文件",Name,"*.mp3");

  QUrl url(Name);
  player.setMedia(url);
  player.play();*/

  /*QFileDialog fileDialog(this);
  fileDialog.setAcceptMode(QFileDialog::AcceptOpen);
  fileDialog.setWindowTitle("打开文件");
  fileDialog.setDirectory(QStandardPaths::standardLocations(QStandardPaths::MusicLocation).value(0,QDir::homePath()));
  QUrl url=fileDialog.selectedUrls().constFirst();*/
  /*player.setMedia(url);
  player.play();*/

  player.setMedia(QUrl::fromLocalFile("E:\\剪辑好的视频\\音乐\\你D起来真好听.mp3"));
  player.play();
}

















